package com.xuyiwei.mayi.test;

import org.jboss.netty.bootstrap.ClientBootstrap;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.*;
import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.string.StringDecoder;
import org.jboss.netty.handler.codec.string.StringEncoder;

import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.channels.FileChannel;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by xuyiwei on 2019/4/15.
 */
class Nio3_3_Final_ServerHandler extends SimpleChannelHandler{
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        super.messageReceived(ctx, e);
        System.out.println("服务端接受到消息"+e.getMessage());
        ctx.getChannel().write("好的");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        super.exceptionCaught(ctx, e);
        System.out.println("exceptionCaught");

    }

    @Override
    public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        super.channelDisconnected(ctx, e);
        System.out.println("channelDisconnected");

    }

    @Override
    public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        super.channelClosed(ctx, e);
        System.out.println("channelClosed");

    }
}
class Nio3_3_Final_Server{
    public static void startServer(){
        //创建服务对象
        ServerBootstrap serverBootstrap = new ServerBootstrap();
        //创建两个线程池 一个监听端口 一个监听nio
        ExecutorService bootServer = Executors.newCachedThreadPool();
        ExecutorService nioServer = Executors.newCachedThreadPool();
        //设置服务对象工厂，并且把两个线程池放入
        serverBootstrap.setFactory(new NioServerSocketChannelFactory(bootServer,nioServer));
        //设置管道工厂
        serverBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline pipeline = Channels.pipeline();
                //nio字符集设置成String
                pipeline.addLast("encoder",new StringEncoder());
                pipeline.addLast("decoder",new StringDecoder());
                pipeline.addLast("serverHandler",new Nio3_3_Final_ServerHandler());
                return pipeline;
            }
        });
        //绑定端口
        serverBootstrap.bind(new InetSocketAddress(8080));
        System.out.println("服务端已经启动");
    }
}



class Nio3_3_Final_ClientHandler extends SimpleChannelHandler{
    @Override
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        super.messageReceived(ctx, e);
        System.out.println("客户端接受到消息"+e.getMessage());
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
        super.exceptionCaught(ctx, e);
        System.out.println("exceptionCaught");

    }

    @Override
    public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        super.channelDisconnected(ctx, e);
        System.out.println("channelDisconnected");

    }

    @Override
    public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
        super.channelClosed(ctx, e);
        System.out.println("channelClosed");

    }
}

class Nio3_3_Final_Client{
    public static void startClient(){
        //启动客户端服务
        ClientBootstrap clientBootstrap = new ClientBootstrap();
        //启动两个线程池 监听端口和nio
        ExecutorService bootServer = Executors.newCachedThreadPool();
        ExecutorService nioServer = Executors.newCachedThreadPool();
        //设置工厂
        clientBootstrap.setFactory(new NioClientSocketChannelFactory(bootServer,nioServer));
        //设置管道工厂
        clientBootstrap.setPipelineFactory(new ChannelPipelineFactory() {
            @Override
            public ChannelPipeline getPipeline() throws Exception {
                ChannelPipeline pipeline = Channels.pipeline();
                pipeline.addLast("decoder",new StringDecoder());
                pipeline.addLast("encoder",new StringEncoder());
                pipeline.addLast("clientHandler",new Nio3_3_Final_ClientHandler());
                return pipeline;
            }
        });
        //连接服务端
        ChannelFuture future = clientBootstrap.connect(new InetSocketAddress("127.0.0.1",8080));
        Channel outChanel = future.getChannel();
        System.out.println("client start");
        Scanner scanner=	new Scanner(System.in);
        while (true) {
            System.out.println("请输输入内容...");
            outChanel.write(scanner.next());
        }
    }
}
public class Nio3_3_Final {
    public static void main(String[] args) throws InterruptedException {
        Nio3_3_Final_Server.startServer();
        Thread.sleep(1000);
        Nio3_3_Final_Client.startClient();

        Nio3_3_Final_Client.startClient();
    }
}
